/**
 * Created by bdbs on 2017/6/20.
 */
const electron = require('electron');
const { Tray, Menu, app } = electron;

const menuConfig = Menu.buildFromTemplate([{
  label: 'Quit',
  accelerator: 'Command+Q',
  click: () => app.quit(),
}])

class TimerTray extends Tray {
  constructor(trayIcon, mainWindow) {
    super(trayIcon);

    this.mainWindow = mainWindow;

    this.on('click', this.handleClick.bind(this));
    this.on('right-click', this.handleRightClick.bind(this));
    this.setToolTip('timer tray');
  }

  handleClick(event, bounces) {
    const { mainWindow } = this;

    if (mainWindow.isVisible()) {
      mainWindow.hide();
    } else {
      const { x, y } = bounces;
      const { width, height } = mainWindow.getBounds();

      mainWindow.setBounds({
        x: x - width / 2,
        y,
        width,
        height,
      });

      mainWindow.show();
    }
  }

  handleRightClick() {
    this.popUpContextMenu(menuConfig);
  }
}

module.exports = TimerTray;
